//
//  ViewController.m
//  汤姆猫动画效果16.3.10
//
//  Created by zhangxin on 16/3/10.
//  Copyright © 2016年 zhangxin. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageViewCat;

- (IBAction)eat;

- (IBAction)cymbal;

- (IBAction)pie;
- (IBAction)knockout;
- (IBAction)fart;
- (IBAction)drink;
- (IBAction)scratch;
- (IBAction)angry;
- (IBAction)stomach;
- (IBAction)foot_right;
- (IBAction)foot_letf;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)startAnimationting:(int)count picName:(NSString *)picName
{
    //判断当前动画是否正在执行，如果正在执行该次点击无效
    if (self.imageViewCat.isAnimating == 1) {
        return;
    }
    
    //加载图片到帧动画数组中
    NSMutableArray * arrayM = [NSMutableArray array];
    //拼接图片名字
    for (int i = 0; i<count; i++) {
        NSString *imgName = [NSString stringWithFormat:@"%@_%02d.jpg",picName,i];
        //根据图片名字得到图片
        //UIImage * imgCat = [UIImage imageNamed:imgName];
        NSString * path = [[NSBundle mainBundle]pathForResource:imgName ofType:nil];
        UIImage * imgCat = [UIImage imageWithContentsOfFile:path];
        //将每次获取的图片放到数组中去
        [arrayM addObject:imgCat];
    }
    //2.将得到的帧动画数组赋值给UIImageView的帧动画数组属性
    self.imageViewCat.animationImages = arrayM;
    //3.设置动画播放时间
    self.imageViewCat.animationDuration = self.imageViewCat.animationImages.count * 0.1;
    //4.设置动画循坏次数
    self.imageViewCat.animationRepeatCount = 1;
    //5.开始播放
    [self.imageViewCat startAnimating];
    //6.动画播完之后清空缓存
    //self.imageViewCat.animationImages = nil;
    [self.imageViewCat performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.imageViewCat.animationImages.count * 0.1];
}


- (IBAction)eat
{
    [self startAnimationting:39 picName:@"eat"];
    }

- (IBAction)cymbal
{
    [self startAnimationting:12 picName:@"cymbal"];
}

- (IBAction)pie
{
    [self startAnimationting:23 picName:@"pie"];
    }

- (IBAction)knockout
{
    [self startAnimationting:80 picName:@"knockout"];
}

- (IBAction)fart
{
    [self startAnimationting:27 picName:@"fart"];
}

- (IBAction)drink
{
    [self startAnimationting:80 picName:@"drink"];
}

- (IBAction)scratch
{
    [self startAnimationting:55 picName:@"scratch"];
}

- (IBAction)angry
{
    [self startAnimationting:25 picName:@"angry"];
}

- (IBAction)stomach
{
    [self startAnimationting:33 picName:@"stomach"];
}

- (IBAction)foot_right
{
    [self startAnimationting:29 picName:@"foot_right"];
}

- (IBAction)foot_letf
{
    [self startAnimationting:29 picName:@"foot_left"];
}


@end
